package org.example.combine;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class ConnectDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

//        DataStreamSource<Integer> source1 = env.fromElements(1, 2, 3);
//        DataStreamSource<Integer> source2 = env.fromElements(11, 22, 33);
//        DataStreamSource<String> source3 = env.fromElements("111", "222", "333");
        SingleOutputStreamOperator<Integer> source1 = env.socketTextStream("localhost", 9999).map(value -> Integer.parseInt(value));
        DataStreamSource<String> source2 = env.socketTextStream("localhost", 8888);

        ConnectedStreams<Integer, String> connect = source1.connect(source2);
        connect.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return "数字流:" + value.toString();
            }

            @Override
            public String map2(String value) throws Exception {
                return "字母流：" + value;
            }
        }).print();
        env.execute("ConnectDemo");
    }
}
